package com.xiaoyu.lanling.feature.im.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.base.utils.upload.UploadListener
import com.xiaoyu.base.utils.upload.UploadMediaType.UploadMediaTypeDef
import com.xiaoyu.base.utils.upload.UploadParam
import com.xiaoyu.im.datamodel.AppMessage
import com.xiaoyu.lanling.common.Constant.MessageAttrsKey.RISK_INFO
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.util.upload.UploadCategory

object IMData {
    fun uploadResourceAsync(chatId: String, @UploadMediaTypeDef mediaType: String, filePath: String, originalType: Int, uploadListener: UploadListener) {
        UploadParam(filePath, UploadCategory.IM, mediaType, chatId, originalType).upload(uploadListener)
    }

    fun checkContentRiskSync(message: AppMessage, messageId: String, messageType: Int, content: String) {
        RequestCreator.create(JsonData::class.java)
                .setRequestUrl(HttpConfig.IM_CHAT_RISK_CHECK)
                .addQueryData("messageId", messageId)
                .addQueryData("messageType", messageType)
                .addQueryData("content", content)
                .setRequestHandler(object : RequestDefaultHandler<JsonData, JsonData>() {
                    override fun processOriginData(originData: JsonData): JsonData {
                        val jsonData = originData.optJson("data")
                        message.payload.putAttrs(RISK_INFO, jsonData)
                        return jsonData
                    }
                })
                .requestSync()
    }
}